/*
* $Id: AddFamilyView.cpp 73 2010-03-25 17:02:36Z volok-aleksej@yandex.ru $
*
* Copyright 2009 by Volokitin Alexey
* All rights reserved
*
*/
#include "Common/include.h"
#include "AddFamilyView.h"
#include "ViewFieldValuesHelper.h"
#include "../../HousingDialog.h"
#include "Visual/Drawers/EditorDrawer/HeaderItem.h"
#include "Visual/Drawers/EditorDrawer/EditItem.h"
#include "Visual/Drawers/EditorDrawer/ComboBoxItem.h"
#include "Settings/Journal/FamilyViewSettings.h"
#include "Settings/SettingControlIds.h"
#include "Settings/DinIntDefined.h"
#include "Database/Tables/FamiliesTable.h"
#include "Database/Tables/HousingsTable.h"
#include "Database/Operation/AddFamilyOperation.h"

#define TITLE_ID			1
#define NAME_FAMILY_ID		2
#define HOUSING_FAMILY_ID	3
#define FAMILY_TYPE_ID		4
#define NOTE_FAMILY_ID		5
#define USER_FIELD_ID		6

CAddFamilyView::CAddFamilyView(CState* pState)
: CAddChangeView(pState), m_nUserFields(0){}
CAddFamilyView::~CAddFamilyView(){}

void CAddFamilyView::Init(){
	m_editorDrawer.Clear();
	m_editorDrawer.AddItem(new CHeaderItem(IApplication::GetApp()->GetLanguageManager()->GetString(ADD_FAMILY_ID), TITLE_ID));
	m_editorDrawer.AddItem(new CEditItem(IApplication::GetApp()->GetLanguageManager()->GetString(TABLE_HEADER_FAMILY_NAME_ID), NAME_FAMILY_ID, editItem::TEXT));
	CComboBoxItem* pHousings = new CComboBoxItem(IApplication::GetApp()->GetLanguageManager()->GetString(HOUSING_NAME_ID), HOUSING_FAMILY_ID);
	AddFreeHousing(pHousings);
	m_editorDrawer.AddItem(pHousings);
	CFamilyViewSettings familyStn;
	if(familyStn.GetCurrentValue(CHECK_TYPE_ID) == CHECK_BOX_STATE_1){
		CComboBoxItem* pItem = new CComboBoxItem(IApplication::GetApp()->GetLanguageManager()->GetString(TABLE_HEADER_FAMILY_TYPE_ID), FAMILY_TYPE_ID);
		AddTypesFamily(pItem);
		m_editorDrawer.AddItem(pItem);
	}
	if(familyStn.GetCurrentValue(CHECK_NOTE_ID) == CHECK_BOX_STATE_1)
		m_editorDrawer.AddItem(new CEditItem(IApplication::GetApp()->GetLanguageManager()->GetString(TABLE_HEADER_NOTE_ID), NOTE_FAMILY_ID, editItem::TEXT));
	CViewFieldValuesHelper helper(&m_editorDrawer);
	m_nUserFields = helper.GenerateUserEditItems(CViewFieldValuesHelper::FAMILIES, USER_FIELD_ID, 0);
}

void CAddFamilyView::SetState(){
	m_pState->m_nState = JournalEnum::VIEW_ADD_FAMILY;
}
bool CAddFamilyView::IsViewState(State state){
	return state.m_type == JournalEnum::ADD_FAMILY;
}

BEGIN_MESSAGE_MAP(CAddFamilyView, CAddChangeView)
	ON_COMMAND(BUTTON_SAVE_ID, OnSaveFamily)
END_MESSAGE_MAP()

void CAddFamilyView::OnSaveFamily(){
	std::tstring strIdHouse = m_editorDrawer.GetValueItem(HOUSING_FAMILY_ID);
	int nIdHousing = _ttoi(strIdHouse.c_str());
	if(!nIdHousing){
		CHousingDialog dialog;
		if(dialog.DoModal() == IDCANCEL)
			return;
		nIdHousing = dialog.GetIdHousing();
	}
	CAddFamilyOperation operation;
	operation.m_content.s_nIdPoint = m_nIdParameter;
	operation.m_content.s_strName = m_editorDrawer.GetValueItem(NAME_FAMILY_ID);
	operation.m_content.s_strNote = m_editorDrawer.GetValueItem(NOTE_FAMILY_ID);
	operation.m_content.s_nType = _ttoi(m_editorDrawer.GetValueItem(FAMILY_TYPE_ID).c_str());
	operation.m_content.s_nIdQueenBee = 0;
	operation.m_content.s_nIdHousing = nIdHousing;
	for(int i = 1; i <= m_nUserFields; i++)
		operation.m_content.s_aUserFields.insert(std::make_pair(m_editorDrawer.GetNameItem(USER_FIELD_ID + i), m_editorDrawer.GetValueItem(USER_FIELD_ID + i)));
	IDatabase* pDb = IApplication::GetApp()->GetDatabaseManager()->OpenDatabase(JOURNAL_DATABASE_NAME);
	operation.AddValue(pDb);
	IApplication::GetApp()->GetDatabaseManager()->CloseDatabase(pDb);
	IWnd* pWnd = IApplication::GetApp()->GetVisualManager()->
				GetVisualComponent(IApplication::GetApp()->
				GetLanguageManager()->GetString(JOURNAL_CAPTION_ID))->GetComponentFrame();
	pWnd->Post(UM_ITEM_MANIPULATION, JournalEnum::ADD_INSPECT, operation.m_content.s_nId);
}

void CAddFamilyView::AddTypesFamily(CComboBoxItem* pItem){
	pItem->AddValueItem(CFamiliesTable::content::DEFAULT, IApplication::GetApp()->GetLanguageManager()->GetString(TYPE_DEFAULT_ID));
	pItem->AddValueItem(CFamiliesTable::content::PEDIGREE, IApplication::GetApp()->GetLanguageManager()->GetString(TYPE_PEDIGREE_ID));
	pItem->AddValueItem(CFamiliesTable::content::BREEDING, IApplication::GetApp()->GetLanguageManager()->GetString(TYPE_BREEDING_ID));
	pItem->AddValueItem(CFamiliesTable::content::FATHER, IApplication::GetApp()->GetLanguageManager()->GetString(TYPE_FATHER_ID));
	pItem->AddValueItem(CFamiliesTable::content::AMADOU, IApplication::GetApp()->GetLanguageManager()->GetString(TYPE_AMADOU_ID));
	pItem->AddValueItem(CFamiliesTable::content::BARREN, IApplication::GetApp()->GetLanguageManager()->GetString(TYPE_BARREN_ID));
	pItem->AddValueItem(CFamiliesTable::content::QUEEN_CELL, IApplication::GetApp()->GetLanguageManager()->GetString(TYPE_QUEEN_CELL_ID));
	pItem->AddValueItem(CFamiliesTable::content::QUEEN_CELL_SMALL, IApplication::GetApp()->GetLanguageManager()->GetString(TYPE_QUEEN_CELL_SMALL_ID));
	pItem->AddValueItem(CFamiliesTable::content::INDIVIDUAL_PROVINE, IApplication::GetApp()->GetLanguageManager()->GetString(TYPE_INDIVIDUAL_PROVINE_ID));
	pItem->AddValueItem(CFamiliesTable::content::COMBINED_PROVINE, IApplication::GetApp()->GetLanguageManager()->GetString(TYPE_COMBINED_PROVINE_ID));
	pItem->AddValueItem(CFamiliesTable::content::PACKET, IApplication::GetApp()->GetLanguageManager()->GetString(TYPE_PACKET_ID));
	pItem->AddValueItem(CFamiliesTable::content::NON_QUEEN, IApplication::GetApp()->GetLanguageManager()->GetString(TYPE_NON_QUEEN_ID));
	pItem->AddValueItem(CFamiliesTable::content::SWARM, IApplication::GetApp()->GetLanguageManager()->GetString(TYPE_SWARM_ID));
	pItem->AddValueItem(CFamiliesTable::content::USER, IApplication::GetApp()->GetLanguageManager()->GetString(TYPE_USER_ID));
}

void CAddFamilyView::AddFreeHousing(CComboBoxItem* pItem){
	pItem->AddValueItem(0, IApplication::GetApp()->GetLanguageManager()->GetString(NEW_HOUSING_ID));
	IDatabase* pDb = IApplication::GetApp()->GetDatabaseManager()->OpenDatabase(JOURNAL_DATABASE_NAME);
	CHousingsTable table;
	table.m_content.s_nIdFamily = 0;
	IResult* pResult = table.GetValue(pDb);
	for (int i = 0; i < (int)pResult->GetSizeResult(); i++){
		CHousingsTable::content* pContent = (CHousingsTable::content*)(*pResult)[i];
		pItem->AddValueItem(pContent->s_nId, pContent->s_strName);
	}
	pResult->Free();
	IApplication::GetApp()->GetDatabaseManager()->CloseDatabase(pDb);
}